<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>MongoDB 查询条件 | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/part2/8.MongoDBIndex.html" />
    
    
    <link rel="prev" href="../../home/part2/6.MongoDBRetrieve.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.7"
        data-chapter-title="MongoDB 查询条件"
        data-filepath="home/part2/7.MongoDB查询条件.md"
        data-basepath="../.."
        data-revision="Sun Oct 21 2018 20:49:12 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="home/NoSQL简介.html">
            
                
                    <a href="../../home/NoSQL简介.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        NoSQL介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="home/part1/Redis.html">
            
                
                    <a href="../../home/part1/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Redis
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/part1/1.常用命令.html">
            
                
                    <a href="../../home/part1/1.常用命令.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        常用命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/part1/2.Redis的数据类型.html">
            
                
                    <a href="../../home/part1/2.Redis的数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Redis的数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="home/part1/3.Redis高级.html">
            
                
                    <a href="../../home/part1/3.Redis高级.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Redis高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="home/part1/4.python操作redis.html">
            
                
                    <a href="../../home/part1/4.python操作redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        python操作redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="home/part2/MongoDB.html">
            
                
                    <a href="../../home/part2/MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        MongoDB
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/part2/1.MongoDB基础操作.html">
            
                
                    <a href="../../home/part2/1.MongoDB基础操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        MongoDB基础操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/part2/2.MongoDBDBS.html">
            
                
                    <a href="../../home/part2/2.MongoDBDBS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        MongoDB DBS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/part2/3.MongoDBCreate.html">
            
                
                    <a href="../../home/part2/3.MongoDBCreate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        MongoDB Create
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="home/part2/4.MongoDBUpdate.html">
            
                
                    <a href="../../home/part2/4.MongoDBUpdate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        MongoDB Update
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="home/part2/5.MongoDBDelete.html">
            
                
                    <a href="../../home/part2/5.MongoDBDelete.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        MongoDB Delete
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="home/part2/6.MongoDBRetrieve.html">
            
                
                    <a href="../../home/part2/6.MongoDBRetrieve.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        MongoDB Retrieve
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.7" data-path="home/part2/7.MongoDB查询条件.html">
            
                
                    <a href="../../home/part2/7.MongoDB查询条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        MongoDB 查询条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="home/part2/8.MongoDBIndex.html">
            
                
                    <a href="../../home/part2/8.MongoDBIndex.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        MongoDB Index
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="home/part2/9.MongoDBDump.html">
            
                
                    <a href="../../home/part2/9.MongoDBDump.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        MongoDB Dump
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="home/part2/10.Python操作MongoDB.html">
            
                
                    <a href="../../home/part2/10.Python操作MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        Python操作MongoDB
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="mongodb&#x67E5;&#x8BE2;&#x6761;&#x4EF6;">MongoDB&#x67E5;&#x8BE2;&#x6761;&#x4EF6;</h1>
<hr>
<ul>
<li><h3 id="mongodb&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;">MongoDB&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;</h3>
<ul>
<li><h4 id="&#x63CF;&#x8FF0;">&#x63CF;&#x8FF0;</h4>
<p>  &#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;&#x7528;&#x4E8E;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x5E76;&#x4ECE;mongoDB&#x96C6;&#x5408;&#x4E2D;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<p>  &#x5728;&#x672C;&#x7AE0;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x8BA8;&#x8BBA;&#x5982;&#x4F55;&#x5728;MongoDB&#x4E2D;&#x4F7F;&#x7528;&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;&#x3002;</p>
<p>  MongoDB&#x4E2D;&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;&#x6709;&#xFF1A;</p>
<pre><code class="lang-js">  $gt  ----&#x5927;&#x4E8E; ---- greater than  &gt;

  $gte ----&#x5C0F;&#x4E8E;----- gt equal      &gt;=

  $lt  ---&#x5927;&#x4E8E;&#x7B49;&#x4E8E;----- less than  &lt;

  $lte ---&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;------ lt equal  &lt;=

  $ne  ---&#x4E0D;&#x7B49;&#x4E8E;------ not equal   !=

  $eq  ----&#x7B49;&#x4E8E;----  equal          =
</code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="type&#x64CD;&#x4F5C;&#x7B26;">$type&#x64CD;&#x4F5C;&#x7B26;</h2>
<p>&#x5728;&#x672C;&#x7AE0;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x7EE7;&#x7EED;&#x8BA8;&#x8BBA;MongoDB&#x4E2D;&#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26; $type&#x3002;</p>
<p>$type&#x64CD;&#x4F5C;&#x7B26;&#x662F;&#x57FA;&#x4E8E;BSON&#x7C7B;&#x578B;&#x6765;&#x68C0;&#x7D22;&#x96C6;&#x5408;&#x4E2D;&#x5339;&#x914D;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</p>
<p>MongoDB &#x4E2D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#x578B;&#x5982;&#x4E0B;&#x8868;&#x6240;&#x793A;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x7C7B;&#x578B;</th>
<th>&#x6570;&#x5B57;</th>
<th>&#x5907;&#x6CE8;</th>
</tr>
</thead>
<tbody>
<tr>
<td>Double</td>
<td>1</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>String</td>
<td>2</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Object</td>
<td>3</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Array</td>
<td>4</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Binary</td>
<td>data    5</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Undefined</td>
<td>6</td>
<td>&#x5DF2;&#x5E9F;&#x5F03;&#x3002;</td>
</tr>
<tr>
<td>Object id</td>
<td>7</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Boolean</td>
<td>8</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Date</td>
<td>9</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Null</td>
<td>10</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Regular Expression</td>
<td>11</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>JavaScript</td>
<td>13</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Symbol</td>
<td>14</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>JavaScript (with scope)</td>
<td>15</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>32-bit integer</td>
<td>16</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Timestamp</td>
<td>17</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>64-bit integer</td>
<td>18</td>
<td>&apos;&apos;</td>
</tr>
<tr>
<td>Min key</td>
<td>255</td>
<td>Query with-1.</td>
</tr>
<tr>
<td>Max key</td>
<td>127</td>
<td>&apos;&apos;</td>
</tr>
</tbody>
</table>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;&#x540D;&#x79F0;&#x4E3A;&quot;runoob&quot; &#x6211;&#x4EEC;&#x7684;&#x96C6;&#x5408;&#x540D;&#x79F0;&#x4E3A;&quot;col&quot;&#xFF0C;&#x4EE5;&#x4E0B;&#x4E3A;&#x6211;&#x4EEC;&#x63D2;&#x5165;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-sql">&gt;db.col.insert({
    title: &apos;PHP &#x6559;&#x7A0B;&apos;, 
    description: &apos;PHP &#x662F;&#x4E00;&#x79CD;&#x521B;&#x5EFA;&#x52A8;&#x6001;&#x4EA4;&#x4E92;&#x6027;&#x7AD9;&#x70B9;&#x7684;&#x5F3A;&#x6709;&#x529B;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;&#x8BED;&#x8A00;&#x3002;&apos;,
    by: &apos;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&apos;,
    url: &apos;http://www.runoob.com&apos;,
    tags: [&apos;php&apos;],
    likes: 200
})
&gt;db.col.insert({title: &apos;Java &#x6559;&#x7A0B;&apos;, 
    description: &apos;Java &#x662F;&#x7531;Sun Microsystems&#x516C;&#x53F8;&#x4E8E;1995&#x5E74;5&#x6708;&#x63A8;&#x51FA;&#x7684;&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;&#x3002;&apos;,
    by: &apos;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&apos;,
    url: &apos;http://www.runoob.com&apos;,
    tags: [&apos;java&apos;],
    likes: 150
})
&gt;db.col.insert({title: &apos;MongoDB &#x6559;&#x7A0B;&apos;, 
    description: &apos;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&apos;,
    by: &apos;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&apos;,
    url: &apos;http://www.runoob.com&apos;,
    tags: [&apos;mongodb&apos;],
    likes: 100
})
</code></pre>
<p>&#x4F7F;&#x7528;find()&#x547D;&#x4EE4;&#x67E5;&#x770B;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-sql">&gt; db.col.find()
{ &quot;_id&quot; : ObjectId(&quot;56066542ade2f21f36b0313a&quot;), &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;PHP &#x662F;&#x4E00;&#x79CD;&#x521B;&#x5EFA;&#x52A8;&#x6001;&#x4EA4;&#x4E92;&#x6027;&#x7AD9;&#x70B9;&#x7684;&#x5F3A;&#x6709;&#x529B;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }
{ &quot;_id&quot; : ObjectId(&quot;56066549ade2f21f36b0313b&quot;), &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;Java &#x662F;&#x7531;Sun Microsystems&#x516C;&#x53F8;&#x4E8E;1995&#x5E74;5&#x6708;&#x63A8;&#x51FA;&#x7684;&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }
{ &quot;_id&quot; : ObjectId(&quot;5606654fade2f21f36b0313c&quot;), &quot;title&quot; : &quot;MongoDB &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }
</code></pre>
<h2 id="type-&#x5B9E;&#x4F8B;">$type &#x5B9E;&#x4F8B;</h2>
<p>&#x5982;&#x679C;&#x60F3;&#x83B7;&#x53D6; &quot;col&quot; &#x96C6;&#x5408;&#x4E2D; title &#x4E3A; String &#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code class="lang-sql">db.col.find({&quot;title&quot; : {$type : 2}})

{ &quot;_id&quot; : ObjectId(&quot;56066542ade2f21f36b0313a&quot;), &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;PHP &#x662F;&#x4E00;&#x79CD;&#x521B;&#x5EFA;&#x52A8;&#x6001;&#x4EA4;&#x4E92;&#x6027;&#x7AD9;&#x70B9;&#x7684;&#x5F3A;&#x6709;&#x529B;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }
{ &quot;_id&quot; : ObjectId(&quot;56066549ade2f21f36b0313b&quot;), &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;Java &#x662F;&#x7531;Sun Microsystems&#x516C;&#x53F8;&#x4E8E;1995&#x5E74;5&#x6708;&#x63A8;&#x51FA;&#x7684;&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }
{ &quot;_id&quot; : ObjectId(&quot;5606654fade2f21f36b0313c&quot;), &quot;title&quot; : &quot;MongoDB &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }
</code></pre>
<h2 id="mongodb-limit-&#x65B9;&#x6CD5;">MongoDB Limit() &#x65B9;&#x6CD5;</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x5728;MongoDB&#x4E2D;&#x8BFB;&#x53D6;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x6570;&#x636E;&#x8BB0;&#x5F55;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;MongoDB&#x7684;Limit&#x65B9;&#x6CD5;&#xFF0C;limit()&#x65B9;&#x6CD5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x53C2;&#x6570;&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x4ECE;MongoDB&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#x8BB0;&#x5F55;&#x6761;&#x6570;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>limit()&#x65B9;&#x6CD5;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.COLLECTION_NAME.find().limit(NUMBER)
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x96C6;&#x5408; col &#x4E2D;&#x7684;&#x6570;&#x636E;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">{ &quot;_id&quot; : ObjectId(&quot;56066542ade2f21f36b0313a&quot;), &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;PHP &#x662F;&#x4E00;&#x79CD;&#x521B;&#x5EFA;&#x52A8;&#x6001;&#x4EA4;&#x4E92;&#x6027;&#x7AD9;&#x70B9;&#x7684;&#x5F3A;&#x6709;&#x529B;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }
{ &quot;_id&quot; : ObjectId(&quot;56066549ade2f21f36b0313b&quot;), &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;Java &#x662F;&#x7531;Sun Microsystems&#x516C;&#x53F8;&#x4E8E;1995&#x5E74;5&#x6708;&#x63A8;&#x51FA;&#x7684;&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }
{ &quot;_id&quot; : ObjectId(&quot;5606654fade2f21f36b0313c&quot;), &quot;title&quot; : &quot;MongoDB &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x4E3A;&#x663E;&#x793A;&#x67E5;&#x8BE2;&#x6587;&#x6863;&#x4E2D;&#x7684;&#x4E24;&#x6761;&#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-sql">&gt; db.col.find({},{&quot;title&quot;:1,_id:0}).limit(2)
{ &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot; }
{ &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot; }
&gt;
</code></pre>
<p>&#x6CE8;&#xFF1A;&#x5982;&#x679C;&#x4F60;&#x4EEC;&#x6CA1;&#x6709;&#x6307;&#x5B9A;limit()&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x5219;&#x663E;&#x793A;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x3002;</p>
<hr>
<h2 id="mongodb-skip-&#x65B9;&#x6CD5;">MongoDB Skip() &#x65B9;&#x6CD5;</h2>
<p>&#x6211;&#x4EEC;&#x9664;&#x4E86;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;limit()&#x65B9;&#x6CD5;&#x6765;&#x8BFB;&#x53D6;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x6570;&#x636E;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;skip()&#x65B9;&#x6CD5;&#x6765;&#x8DF3;&#x8FC7;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x6570;&#x636E;&#xFF0C;skip&#x65B9;&#x6CD5;&#x540C;&#x6837;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x53C2;&#x6570;&#x4F5C;&#x4E3A;&#x8DF3;&#x8FC7;&#x7684;&#x8BB0;&#x5F55;&#x6761;&#x6570;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>skip() &#x65B9;&#x6CD5;&#x811A;&#x672C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x53EA;&#x4F1A;&#x663E;&#x793A;&#x7B2C;&#x4E8C;&#x6761;&#x6587;&#x6863;&#x6570;&#x636E;</p>
<pre><code class="lang-sql">&gt;db.col.find({},{&quot;title&quot;:1,_id:0}).limit(1).skip(1)
{ &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot; }
&gt;
</code></pre>
<p>&#x6CE8;:skip()&#x65B9;&#x6CD5;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x4E3A; 0 &#x3002;</p>
<hr>
<h2 id="mongodb-&#x6392;&#x5E8F;-sort&#x65B9;&#x6CD5;">MongoDB &#x6392;&#x5E8F; sort()&#x65B9;&#x6CD5;</h2>
<p>&#x5728;MongoDB&#x4E2D;&#x4F7F;&#x7528;&#x4F7F;&#x7528;sort()&#x65B9;&#x6CD5;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;sort()&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x5B57;&#x6BB5;&#xFF0C;&#x5E76;&#x4F7F;&#x7528; 1 &#x548C; -1 &#x6765;&#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5176;&#x4E2D; 1 &#x4E3A;&#x5347;&#x5E8F;&#x6392;&#x5217;&#xFF0C;&#x800C;-1&#x662F;&#x7528;&#x4E8E;&#x964D;&#x5E8F;&#x6392;&#x5217;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>sort()&#x65B9;&#x6CD5;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.COLLECTION_NAME.find().sort({KEY:1})
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>col &#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">{ &quot;_id&quot; : ObjectId(&quot;56066542ade2f21f36b0313a&quot;), &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;PHP &#x662F;&#x4E00;&#x79CD;&#x521B;&#x5EFA;&#x52A8;&#x6001;&#x4EA4;&#x4E92;&#x6027;&#x7AD9;&#x70B9;&#x7684;&#x5F3A;&#x6709;&#x529B;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x811A;&#x672C;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;php&quot; ], &quot;likes&quot; : 200 }
{ &quot;_id&quot; : ObjectId(&quot;56066549ade2f21f36b0313b&quot;), &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;Java &#x662F;&#x7531;Sun Microsystems&#x516C;&#x53F8;&#x4E8E;1995&#x5E74;5&#x6708;&#x63A8;&#x51FA;&#x7684;&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;&#x3002;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;java&quot; ], &quot;likes&quot; : 150 }
{ &quot;_id&quot; : ObjectId(&quot;5606654fade2f21f36b0313c&quot;), &quot;title&quot; : &quot;MongoDB &#x6559;&#x7A0B;&quot;, &quot;description&quot; : &quot;MongoDB &#x662F;&#x4E00;&#x4E2A; Nosql &#x6570;&#x636E;&#x5E93;&quot;, &quot;by&quot; : &quot;&#x83DC;&#x9E1F;&#x6559;&#x7A0B;&quot;, &quot;url&quot; : &quot;http://www.runoob.com&quot;, &quot;tags&quot; : [ &quot;mongodb&quot; ], &quot;likes&quot; : 100 }
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x6F14;&#x793A;&#x4E86; col &#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x6309;&#x5B57;&#x6BB5; likes &#x7684;&#x964D;&#x5E8F;&#x6392;&#x5217;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.col.find({},{&quot;title&quot;:1,_id:0}).sort({&quot;likes&quot;:-1})
{ &quot;title&quot; : &quot;PHP &#x6559;&#x7A0B;&quot; }
{ &quot;title&quot; : &quot;Java &#x6559;&#x7A0B;&quot; }
{ &quot;title&quot; : &quot;MongoDB &#x6559;&#x7A0B;&quot; }
&gt;
</code></pre>
<h2 id="mongodb-&#x6392;&#x5E8F;-count&#x65B9;&#x6CD5;">MongoDB &#x6392;&#x5E8F; count()&#x65B9;&#x6CD5;</h2>
<p>&#x5728;MongoDB&#x4E2D;&#x4F7F;&#x7528;&#x4F7F;&#x7528;count()&#x65B9;&#x6CD5;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x7EDF;&#x8BA1;</p>
<p>&#x7C7B;&#x4F3C;sql&#x4E2D; select count(*) from mycol</p>
<pre><code class="lang-sql">&gt; db.mycol.count()
3
</code></pre>
<hr>
<h1 id="mongodb-&#x805A;&#x5408;">MongoDB &#x805A;&#x5408;</h1>
<p>MongoDB&#x4E2D;&#x805A;&#x5408;(aggregate)&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x6570;&#x636E;(&#x8BF8;&#x5982;&#x7EDF;&#x8BA1;&#x5E73;&#x5747;&#x503C;,&#x6C42;&#x548C;&#x7B49;)&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x8BA1;&#x7B97;&#x540E;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x679C;&#x3002;&#x6709;&#x70B9;&#x7C7B;&#x4F3C;sql&#x8BED;&#x53E5;&#x4E2D;&#x7684; count(*)&#x3002;</p>
<hr>
<h2 id="aggregate-&#x65B9;&#x6CD5;">aggregate() &#x65B9;&#x6CD5;</h2>
<p>MongoDB&#x4E2D;&#x805A;&#x5408;&#x7684;&#x65B9;&#x6CD5;&#x4F7F;&#x7528;aggregate()&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>aggregate() &#x65B9;&#x6CD5;&#x7684;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">{
   _id: ObjectId(7df78ad8902c)
   title: &apos;MongoDB Overview&apos;, 
   description: &apos;MongoDB is no sql database&apos;,
   by_user: &apos;runoob.com&apos;,
   url: &apos;http://www.runoob.com&apos;,
   tags: [&apos;mongodb&apos;, &apos;database&apos;, &apos;NoSQL&apos;],
   likes: 100
},
{
   _id: ObjectId(7df78ad8902d)
   title: &apos;NoSQL Overview&apos;, 
   description: &apos;No sql database is very fast&apos;,
   by_user: &apos;runoob.com&apos;,
   url: &apos;http://www.runoob.com&apos;,
   tags: [&apos;mongodb&apos;, &apos;database&apos;, &apos;NoSQL&apos;],
   likes: 10
},
{
   _id: ObjectId(7df78ad8902e)
   title: &apos;Neo4j Overview&apos;, 
   description: &apos;Neo4j is no sql database&apos;,
   by_user: &apos;Neo4j&apos;,
   url: &apos;http://www.neo4j.com&apos;,
   tags: [&apos;neo4j&apos;, &apos;database&apos;, &apos;NoSQL&apos;],
   likes: 750
},
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4EE5;&#x4E0A;&#x96C6;&#x5408;&#x8BA1;&#x7B97;&#x6BCF;&#x4E2A;&#x4F5C;&#x8005;&#x6240;&#x5199;&#x7684;&#x6587;&#x7AE0;&#x6570;&#xFF0C;&#x4F7F;&#x7528;aggregate()&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql"> db.mycol.aggregate([{$group : {_id : &quot;$by_user&quot;, num_tutorial : {$sum : 1}}}])
{
   &quot;result&quot; : [
      {
         &quot;_id&quot; : &quot;runoob.com&quot;,
         &quot;num_tutorial&quot; : 2
      },
      {
         &quot;_id&quot; : &quot;Neo4j&quot;,
         &quot;num_tutorial&quot; : 1
      }
   ],
   &quot;ok&quot; : 1
}
&gt;
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x7C7B;&#x4F3C;sql&#x8BED;&#x53E5;&#xFF1A;select by_user, count(*) from mycol group by by_user</p>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x5B57;&#x6BB5;by_user&#x5B57;&#x6BB5;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#xFF0C;&#x5E76;&#x8BA1;&#x7B97;by_user&#x5B57;&#x6BB5;&#x76F8;&#x540C;&#x503C;&#x7684;&#x603B;&#x548C;&#x3002;</p>
<p>&#x4E0B;&#x8868;&#x5C55;&#x793A;&#x4E86;&#x4E00;&#x4E9B;&#x805A;&#x5408;&#x7684;&#x8868;&#x8FBE;&#x5F0F;:</p>
<table>
<thead>
<tr>
<th>&#x8868;&#x8FBE;&#x5F0F;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>&#x5B9E;&#x4F8B;</th>
</tr>
</thead>
<tbody>
<tr>
<td>$sum</td>
<td>&#x8BA1;&#x7B97;&#x603B;&#x548C;&#x3002;</td>
<td><code>db.mycol.aggregate([{$group : {_id : &quot;$by_user&quot;, num_tutorial : {$sum : &quot;$likes&quot;}}}])</code></td>
</tr>
<tr>
<td>$avg</td>
<td>&#x8BA1;&#x7B97;&#x5E73;&#x5747;&#x503C;</td>
<td><code>db.mycol.aggregate([{$group : {_id : &quot;$by_user&quot;, num_tutorial : {$avg : &quot;$likes&quot;}}}])</code></td>
</tr>
<tr>
<td>$min</td>
<td>&#x83B7;&#x53D6;&#x96C6;&#x5408;&#x4E2D;&#x6240;&#x6709;&#x6587;&#x6863;&#x5BF9;&#x5E94;&#x503C;&#x5F97;&#x6700;&#x5C0F;&#x503C;&#x3002;</td>
<td><code>db.mycol.aggregate([{$group : {_id : &quot;$by_user&quot;, num_tutorial : {$min : &quot;$likes&quot;}}}])</code></td>
</tr>
<tr>
<td>$max</td>
<td>&#x83B7;&#x53D6;&#x96C6;&#x5408;&#x4E2D;&#x6240;&#x6709;&#x6587;&#x6863;&#x5BF9;&#x5E94;&#x503C;&#x5F97;&#x6700;&#x5927;&#x503C;&#x3002;</td>
<td><code>db.mycol.aggregate([{$group : {_id : &quot;$by_user&quot;, num_tutorial : {$max : &quot;$likes&quot;}}}])</code></td>
</tr>
</tbody>
</table>
<pre><code class="lang-sql">aggregate() &#x805A;&#x5408;

db.user.aggregate([{$group:{_id:&apos;$sex&apos;,num:{$sum:1}}}])
{ &quot;_id&quot; : 1, &quot;num&quot; : 9 }
{ &quot;_id&quot; : 0, &quot;num&quot; : 9 }

-- select sex as _id,count(id) num from user group by sex 
+------+-----+
| _id  | num |
+------+-----+
| 0    |   3 |
| 1    |   4 |
+------+-----+

-- select sum(age) from user
db.user.aggregate([{$group:{_id:null,num:{$sum:&apos;$age&apos;}}}])
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/part2/6.MongoDBRetrieve.html" class="navigation navigation-prev " aria-label="Previous page: MongoDB Retrieve"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/part2/8.MongoDBIndex.html" class="navigation navigation-next " aria-label="Next page: MongoDB Index"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
